package com.leixiaoqiao.controller;

import com.github.pagehelper.PageInfo;
import com.leixiaoqiao.api.Result;
import com.leixiaoqiao.pojo.User;
import com.leixiaoqiao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 用户控制器
 * @company: 云桥之上
 * @author: 萌萌居家好男人
 * @version: 1.0
 * @create: 2021-11-21 20:17:27
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result<?> login(@RequestBody User user){
        Result<Object> result = new Result<>();
        Boolean b = userService.login(user);
        if (b){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("登录成功");
            return result;
        }else {
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("登录失败");
            return result;
        }
    }

    @RequestMapping("/getAll")
    public List<User> getAll(){
        List<User> userList=userService.findAll();
        return userList;
    }

    //前端请求地址：http://localhost:8080/SSM/user/getPage?pageNum=2&pageSize=3 请求方式get
    @RequestMapping("/getPage")
    //问号传参
    public Result<?> getPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize){
        Result<Object> result = new Result<>();
        //调用业务方法
        PageInfo<User> pageInfo=userService.getPage(pageNum,pageSize);
        result.setCode(200);//不允许直接写固定值，实际开发中应该使用静态常量代替固定值方式
        result.setSuccess(true);
        result.setData(pageInfo);
        result.setMessage("分页查询成功");
        return result;
    }

    //前端请求地址：http://localhost:8080/SSM/user/page/1/3
    @GetMapping("/page/{pageNum}/{pageSize}")
    //restful风格
    public Result<PageInfo<User>> Page(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Result<PageInfo<User>> result = new Result<>();
        //调用业务方法
        PageInfo<User> pageInfo = userService.page(pageNum,pageSize);
        result.setCode(200);//不允许直接写固定值，实际开发中应该使用静态常量代替固定值方式
        result.setSuccess(true);
        result.setData(pageInfo);
        result.setMessage("分页查询成功");
        return result;
    }

    //新增操作 发post请求在body里面写json数据
    @PostMapping
    public Result<?> add(@RequestBody User user){
        Result<Object> result = new Result<>();
        //调用业务方法新增用户
        Boolean rs = userService.add(user);

        if(rs){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("新增成功");
            return result;
        }else{
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("新增失败");
            return result;
        }
    }

    //查询操作
    @GetMapping("/{id}")
    public Result<?> quary(@PathVariable Integer id){
        Result<Object> result = new Result<>();
        //调用业务方法新增用户
         User user = userService.quary(id);
        if(user!=null){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("查询成功");
            result.setData(user);
            return result;
        }else{
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
    }
}
